package com.my.mvvmrapid.ui.login;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProvider;

import com.github.razir.progressbutton.ButtonTextAnimatorExtensionsKt;
import com.github.razir.progressbutton.DrawableButtonExtensionsKt;
import com.github.razir.progressbutton.ProgressButtonHolderKt;
import com.my.mvvmrapid.AppViewModelFactory;
import com.my.mvvmrapid.BR;
import com.my.mvvmrapid.R;
import com.my.mvvmrapid.databinding.ActivityLoginBinding;
import com.zhongjh.mvvmrapid.base.ui.BaseActivity;
import com.zhongjh.mvvmrapid.utils.ScreenUtil;

import org.jetbrains.annotations.NotNull;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import kotlin.Unit;

/**
 * Created by zhongjh on 2021/3/25.
 */
public class LoginActivity extends BaseActivity<ActivityLoginBinding, LoginViewModel> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ScreenUtil.setFullScreen(LoginActivity.this, false);
        super.onCreate(savedInstanceState);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_login;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public LoginViewModel initViewModel() {
        // 使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        return new ViewModelProvider(this, AppViewModelFactory.getInstance(getApplication())).get(LoginViewModel.class);
    }

    @Override
    public boolean isStartAutoHideSoftKeyboard() {
        return true;
    }

    @Override
    public void initParam() {

    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void initData() {
        ButtonTextAnimatorExtensionsKt.attachTextChangeAnimator(binding.tvLogin);
        ProgressButtonHolderKt.bindProgressButton(this, binding.tvLogin);
    }

    @Override
    public void initViewObservable() {
        viewModel.mUiChange.userNameLoginError.observe(this, s -> binding.txtilLoginAccount.setError(s));
        viewModel.mUiChange.passwordLoginError.observe(this, s -> binding.txtilLoginPassword.setError(s));
        viewModel.mUiChange.userNameRegionError.observe(this, s -> binding.txtilRegionAccount.setError(s));
        viewModel.mUiChange.passwordRegionError.observe(this, s -> binding.txtilRegionPassword.setError(s));
        viewModel.mUiChange.rePasswordRegionError.observe(this, s -> binding.txtilRegionRepassword.setError(s));
        viewModel.mUiChange.showLoginProgress.observe(this, aVoid -> showLoginProgress());
        viewModel.mUiChange.showLoginSucceed.observe(this, aVoid -> showLoginSucceed());
        viewModel.mUiChange.showLoginError.observe(this, aVoid -> showLoginError());
    }

    /**
     * 登录中显示进度
     */
    private void showLoginProgress() {
//        binding.tvLogin.setEnabled(false);
        DrawableButtonExtensionsKt.showProgress(binding.tvLogin, progressParams -> {
            progressParams.setButtonText("登录中");
            progressParams.setProgressColor(Color.WHITE);
            return Unit.INSTANCE;
        });
    }

    /**
     * 登录成功
     */
    private void showLoginSucceed() {
        binding.tvLogin.setEnabled(false);
        // 圆圈
        final Drawable animatedDrawable = ContextCompat.getDrawable(this, R.drawable.animated_check);
        int drawableSize = getResources().getDimensionPixelSize(R.dimen.doneSize);
        if (animatedDrawable != null) {
            animatedDrawable.setBounds(0, 0, drawableSize, drawableSize);
            DrawableButtonExtensionsKt.showDrawable(binding.tvLogin, animatedDrawable, drawableParams -> {
                drawableParams.setButtonText("登录成功");
                return Unit.INSTANCE;
            });
        }
    }

    /**
     * 登录失败
     * strokeWidth 可以描绘线的宽度
     */
    private void showLoginError() {
        // 圆圈
        final Drawable animatedDrawable = ContextCompat.getDrawable(this, R.drawable.animated_check);
        int drawableSize = getResources().getDimensionPixelSize(R.dimen.doneSize);
        if (animatedDrawable != null) {
            animatedDrawable.setBounds(0, 0, drawableSize, drawableSize);
            DrawableButtonExtensionsKt.showDrawable(binding.tvLogin, animatedDrawable, drawableParams -> {
                drawableParams.setButtonText("登录失败");
                resetLogin();
                return Unit.INSTANCE;
            });
        }
    }

    /**
     * 重置登录
     */
    private void resetLogin() {
        Observable.timer(500, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NotNull Disposable d) {
                        viewModel.accept(d);
                    }

                    @Override
                    public void onNext(@NotNull Long value) {
                        // 重置
                        DrawableButtonExtensionsKt.hideDrawable(binding.tvLogin, "登录");
                        binding.tvLogin.setEnabled(true);
                    }

                    @Override
                    public void onError(@NotNull Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }


}
